using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace _0912499_0912581
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        KeyboardState keyboardState;
        KeyboardState oldKeyboardState;

        //GameScreen
        MyGameScreen currentScreen;//****
        GameMenuScreen gameMenuScreen;
        ActionScreen actionScreen;
        PopUpScreen quitScreen;
        PopUpScreen changeLevleScreen;
        PopUpScreen saveGame;
        PopUpScreen loadGame;
        PopUpScreen settingGame;
        OtherScreen winGameScreen;
        OtherScreen gameOverScreen;
        OtherScreen aboutScreen;
        

        GamePadState gamePadState;
        GamePadState oldGamePadState;

        public static int Width;
        public static int Height;

        SoundEffect mainScreenSound;
        SoundEffectInstance instanceSound;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            this.IsMouseVisible = true;
            //graphics.PreferredBackBufferWidth = GetScreenDimensions.X;
            //graphics.PreferredBackBufferHeight = GetScreenDimensions.Y;
            graphics.PreferredBackBufferWidth = 900;
            graphics.PreferredBackBufferHeight = 600;
            graphics.IsFullScreen = true;
            graphics.ApplyChanges();            

            Width = GetScreenDimensions.X;
            Height = GetScreenDimensions.Y;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            //spriteBatch = new SpriteBatch(GraphicsDevice);

            gameMenuScreen = new GameMenuScreen(
                this,
                spriteBatch,
                Content.Load<SpriteFont>("menufont"),
                Content.Load<Texture2D>("Resources\\Background"));
            Components.Add(gameMenuScreen);
            gameMenuScreen.Hide();


            actionScreen = new ActionScreen(
        this,
        spriteBatch,
        Content.Load<Texture2D>("Resources\\bgr"));
            Components.Add(actionScreen);

            actionScreen.LoadContent();
            actionScreen.Hide();



            quitScreen = new PopUpScreen(
                this,
                spriteBatch,
                Content.Load<SpriteFont>("menufont"),
                Content.Load<Texture2D>("Resources\\quitscreen"));
            Components.Add(quitScreen);
            quitScreen.Hide();

            changeLevleScreen = new PopUpScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"),
                Content.Load<Texture2D>("Resources\\ChangeLevelScreen"));
            Components.Add(changeLevleScreen);
            changeLevleScreen.Hide();

            winGameScreen = new OtherScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"),
                Content.Load<Texture2D>("Resources\\WingameScreen"));
            Components.Add(winGameScreen);
            winGameScreen.Hide();

            gameOverScreen = new OtherScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"),
                Content.Load<Texture2D>("Resources\\GameoverScreen"));
           Components.Add(gameOverScreen);
            gameOverScreen.Hide();

            saveGame = new PopUpScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"),
                Content.Load<Texture2D>("Resources\\bg2"));
            Components.Add(saveGame);
            saveGame.Hide();

            loadGame = new PopUpScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"),
                Content.Load<Texture2D>("Resources\\bg2"));
            Components.Add(loadGame);
            loadGame.Hide();

            settingGame = new PopUpScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"),
                Content.Load<Texture2D>("Resources\\bg2"));
            Components.Add(settingGame);
            settingGame.Hide();


            aboutScreen = new OtherScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"),
                Content.Load<Texture2D>("Resources\\bg2"));
            Components.Add(aboutScreen);
            aboutScreen.Hide();


            //activeScreen = startScreen;
            currentScreen = gameMenuScreen;
            currentScreen.Show();

            mainScreenSound = Content.Load<SoundEffect>("Sound\\Audio");
            instanceSound = mainScreenSound.CreateInstance();

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            //// Allows the game to exit
            //if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            //    this.Exit();

            //// TODO: Add your update logic here

            keyboardState = Keyboard.GetState();
            gamePadState = GamePad.GetState(PlayerIndex.One);
            if (currentScreen == gameMenuScreen)
            {
                HandleGameMenuScreen();
                if (instanceSound.State == SoundState.Stopped)
                {
                    instanceSound.Play();
                    //instanceSound.IsLooped = true;
                }
            }
            else//Khong phai man hinh chinh
            {
                instanceSound.Stop();
                if (currentScreen == actionScreen)
                {
                    HandleActionScreen();
                }
                else
                    if (currentScreen == quitScreen)
                    {
                        HandleQuitScreen();
                    }
                    else
                        if (currentScreen == changeLevleScreen)
                        {
                            HandleChanglevelScreen();
                        }
                        else
                            if (currentScreen == winGameScreen)
                            {
                                HandleWinGameScreen();
                            }
                            else
                                if (currentScreen == gameOverScreen)
                                {
                                    HandleGameoverScreen();
                                }
                                //else
                                //    if (currentScreen == saveGame)
                                //    {
                                //        HandleSaveGameScreen();
                                //    }
                                //    else
                                //        if (currentScreen == loadGame)
                                //        {
                                //            HandleLoadGameScreen();
                                //        }
                                //        else
                                //            if (currentScreen == aboutScreen)
                                //            {
                                //                HandleAboutScreen();
                                //            }
                                //            else
                                //                if (currentScreen == settingGame)
                                //                {
                                //                    HandleSettingScreen();
                                //                }
            }

            //currentScreen.Update(gameTime);

            base.Update(gameTime);
            oldKeyboardState = keyboardState;
            oldGamePadState = gamePadState;
        }

        public bool CheckButton(Buttons button)
        {
            return gamePadState.IsButtonUp(button) &&
                oldGamePadState.IsButtonDown(button);
        }

        public void HandleGameMenuScreen()
        {
            if (CheckKey(Keys.Enter) || CheckButton(Buttons.A))
            {
                if (gameMenuScreen.SelectedIndex == (int)GameMenuScreen.MENUSTATE.STARTGAME)
                {
                    currentScreen.Hide();
                    currentScreen = actionScreen;
                    currentScreen.Show();
                }

                if (gameMenuScreen.SelectedIndex == (int)GameMenuScreen.MENUSTATE.EXIT)
                {
                    this.Exit();
                }

                //if (gameMenuScreen.SelectedIndex == (int)GameMenuScreen.MENUSTATE.LOADGAME)
                //{
                //    currentScreen.Hide();
                //    currentScreen = loadGame;
                //    loadGame.Show();
                //}

                //else
                //    if (gameMenuScreen.SelectedIndex == (int)GameMenuScreen.MENUSTATE.SAVEGAME)
                //    {
                //        currentScreen.Hide();
                //        currentScreen = saveGame;
                //        currentScreen.Show();
                //    }
                //    else
                //        if (gameMenuScreen.SelectedIndex == (int)GameMenuScreen.MENUSTATE.SETTING)
                //        {
                //            currentScreen.Hide();
                //            currentScreen = settingGame;
                //            currentScreen.Show();
                //        }
                //        else
                //            if (gameMenuScreen.SelectedIndex == (int)GameMenuScreen.MENUSTATE.ABOUT)
                //            {
                //                currentScreen.Hide();
                //                currentScreen = aboutScreen;
                //                currentScreen.Show();
                //            }

            }
        }
        public void HandleActionScreen()
        {
            if (CheckButton(Buttons.Back) || CheckKey(Keys.Q))
            {
                //currentScreen.Enabled = false;
                currentScreen.Hide();
                currentScreen = quitScreen;
                currentScreen.Show();
            }

            if (ActionScreen.screenSate == (int)ActionScreen.ScreenState.ChangeLevel)
            {
                //currentScreen.Enabled = false;//***
                currentScreen.Hide();
                currentScreen = changeLevleScreen;
                currentScreen.Show();
            }
            else

                if (ActionScreen.screenSate == (int)ActionScreen.ScreenState.WinGame)
                {
                    currentScreen.Hide();
                    currentScreen = winGameScreen;
                    currentScreen.Show();
                }
                else
                    if (ActionScreen.screenSate == (int)ActionScreen.ScreenState.GameOver)
                    {
                        currentScreen.Hide();
                        currentScreen = gameOverScreen;
                        currentScreen.Show();
                    }

        }
        private void HandleQuitScreen()
        {
            if (CheckKey(Keys.Enter) || CheckButton(Buttons.A))
            {
                if (quitScreen.SelectedIndex == (int)PopUpScreen.GAMESTATE.YES)
                {
                    currentScreen.Hide();
                    currentScreen = gameMenuScreen;
                    currentScreen.Show();
                    //actionScreen.Hide();//****
                }
                else
                    if (quitScreen.SelectedIndex == (int)PopUpScreen.GAMESTATE.NO)
                    {
                        currentScreen.Hide();
                        currentScreen = actionScreen;
                        currentScreen.Show();
                    }
            }
        }

        public void HandleChanglevelScreen()
        {
            if (CheckKey(Keys.Enter) || CheckButton(Buttons.A))
            {
                if (changeLevleScreen.SelectedIndex == (int)PopUpScreen.GAMESTATE.YES)
                {
                    currentScreen.Hide();
                    currentScreen = actionScreen;
                    currentScreen.Show();
                    ActionScreen.screenSate = (int)ActionScreen.ScreenState.InGame;
                    
                }
                else
                    if (changeLevleScreen.SelectedIndex == (int)PopUpScreen.GAMESTATE.NO)
                    {
                        currentScreen.Hide();
                        currentScreen = gameMenuScreen;
                        currentScreen.Show();
                        actionScreen.Hide();
                    }

            }
        }

        public void HandleWinGameScreen()
        {
            if (CheckKey(Keys.Enter) || CheckButton(Buttons.A))
            {
                if (winGameScreen.SelectedIndex == (int)OtherScreen.GAMESTATE.OK)
                {
                    currentScreen.Hide();
                    currentScreen = gameMenuScreen;
                    currentScreen.Show();
                    ActionScreen.screenSate = (int)ActionScreen.ScreenState.InGame;
                }
            }
        }

        public void HandleGameoverScreen()
        {
            if (CheckKey(Keys.Enter) || CheckButton(Buttons.A))
            {
                if (winGameScreen.SelectedIndex == (int)OtherScreen.GAMESTATE.OK)
                {
                    currentScreen.Hide();
                    currentScreen = gameMenuScreen;
                    currentScreen.Show();
                    ActionScreen.screenSate = (int)ActionScreen.ScreenState.InGame;
                }

            }
        }

        public void HandleSaveGameScreen()
        {
            if (CheckKey(Keys.Enter) || CheckButton(Buttons.A))
            {
                if (saveGame.SelectedIndex == (int)PopUpScreen.GAMESTATE.YES)
                {

                }
                else
                {
                    if (saveGame.SelectedIndex == (int)PopUpScreen.GAMESTATE.NO)
                    {
                        currentScreen.Hide();
                        currentScreen = gameMenuScreen;
                        currentScreen.Show();
                    }
                }
            }

        }


        public void HandleLoadGameScreen()
        {
            if (CheckKey(Keys.Enter) || CheckButton(Buttons.A))
            {
                if (saveGame.SelectedIndex == (int)PopUpScreen.GAMESTATE.YES)
                {

                }
                else
                {
                    if (saveGame.SelectedIndex == (int)PopUpScreen.GAMESTATE.NO)
                    {
                        currentScreen.Hide();
                        currentScreen = gameMenuScreen;
                        currentScreen.Show();
                    }
                }
            }
        }

        public void HandleAboutScreen()
        {
            if (CheckKey(Keys.Enter) || CheckButton(Buttons.A))
            {
                if (aboutScreen.SelectedIndex == (int)OtherScreen.GAMESTATE.OK)
                {
                    currentScreen.Hide();
                    currentScreen = gameMenuScreen;
                    currentScreen.Show();
                }
            }
        }

        public void HandleSettingScreen()
        {
            if (CheckKey(Keys.Enter) || CheckButton(Buttons.A))
            {
                if (saveGame.SelectedIndex == (int)PopUpScreen.GAMESTATE.YES)
                {

                }
                else
                {
                    if (saveGame.SelectedIndex == (int)PopUpScreen.GAMESTATE.NO)
                    {
                        currentScreen.Hide();
                        currentScreen = gameMenuScreen;
                        currentScreen.Show();
                    }
                }
            }
        }

        private bool CheckKey(Keys theKey)
        {
            return keyboardState.IsKeyDown(theKey) &&
                oldKeyboardState.IsKeyUp(theKey);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.CornflowerBlue);

            //// TODO: Add your drawing code here


            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

            base.Draw(gameTime);
            spriteBatch.End();
        }

        public Point GetScreenDimensions
        {
            get
            {
                return new Point(graphics.GraphicsDevice.DisplayMode.Width, graphics.GraphicsDevice.DisplayMode.Height);
            }
        }



    }
}
